import { defineStore } from 'pinia'
import type { Message, ChatState } from '@/types/chat'

export const useChatStore = defineStore('chat', {
  state: (): ChatState => ({
    messages: [],
    currentUser: '匿名用户',
    connected: false,
    error: null,
  }),
  actions: {
    addMessage(message: Omit<Message, 'timestamp'>): void {
      this.messages.push({
        ...message,
        timestamp: new Date(),
      })
    },
    setConnected(status: boolean): void {
      this.connected = status
    },
    setError(error: Error | null): void {
      this.error = error
    },
    clearMessages(): void {
      this.messages = []
    },
    setUser(username?: string): void {
      this.currentUser = username || '匿名用户'
    },
  },
})
